package Gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import track.Artist;
import track.Song;
import track.Track;
import track.playlistEntry;

public class UpdateSong {
	public Display usDisplay;
	private Shell usShell;
	public UpdateSong(Shell shell, playlistEntry pE, final Main main){
		this.usDisplay = shell.getDisplay();
		this.usShell = new Shell (usDisplay);
		main.addShell(usShell);
		invokeSong(shell, pE, usShell, usDisplay, main);
	}
	public static void invokeSong(Shell shell, final playlistEntry pE, final Shell usShell, Display usDisplay, final Main main){
		GridLayout gl = new GridLayout(1,true);
		usShell.setLayout(gl);
		usShell.setText("Update song");
		usShell.setLocation(shell.getLocation().x + 50, shell.getLocation().y + 50);
	
		Label Explain = new Label(usShell, SWT.NONE);
		Explain.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,true));
		Explain.setText("Details to update:                                             ");
		
		Label Artist = new Label (usShell, SWT.NONE);
		Artist.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,false));
		Artist.setText ("Artist:\t");
		final Text ArtistInput = new Text(usShell,SWT.BORDER);
		ArtistInput.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
	
		Label Song = new Label (usShell, SWT.NONE);
		Song.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,false));
		Song.setText ("Song:\t");
		final Text SongInput = new Text(usShell,SWT.BORDER);
		SongInput.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
		
		ArtistInput.setText(pE.getArtist().getName());
		SongInput.setText(pE.getSong().getName());
		
		final Button updateBt = new Button(usShell,0);
		updateBt.setText("Update");
		updateBt.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,true));
		
		final Button cancelBt = new Button(usShell,0);
		cancelBt.setText("Cancel");
		cancelBt.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,true));
		
		Listener listener = new Listener () {
			public void handleEvent (Event e) {
				switch (e.type) {
				case SWT.Close:
					main.removeShell(usShell);
					usShell.dispose();
					break;
				default:
		        }	
		   }
		};
		usShell.addListener (SWT.Close, listener);
		
		cancelBt.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						main.removeShell(usShell);
						usShell.dispose();
					}
				}
		);
		
		updateBt.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						String tempAr = ArtistInput.getText();
						String tempSong = SongInput.getText();
						if(tempAr == ""){
							tempAr = pE.getArtist().getName();
						}
						if(tempSong == ""){
							tempSong = pE.getSong().getName();
						}
						Artist tempA = pE.getArtist();
						tempA.setName(tempAr);
						Track tempS = pE.getSong();
						tempS.set_name(tempSong);
						pE.updatePE(tempA, tempS, pE.getAlbum());
						main.removeShell(usShell);
						usShell.dispose();
					}
				}
		);
		
		usShell.pack();
		usShell.open();

		while (!usShell.isDisposed()) {
			if (!usDisplay.readAndDispatch())
				usDisplay.sleep();
		}
	}
}
